/*
#author				Chieh-Hsin Fan
#e-mail				chiehhsin.fan@gmail.com
#version			0.1
*/

Array.prototype.findIndex = function(value){
	var ctr = -1;
	for (var i=0; i < this.length; i++) {
	// use === to check for Matches. ie., identical (===), ;
		if (this[i] == value) {
			return i;
		}
	}
	return ctr;
};
var relatedPost = {
		//start of user define
		numRelatedPosts: 1,
		numPostFromRecent: 5,
		//end of user define
		relatedPostsNum: 0,
		relatedTitles: new Array(),
		relatedUrls: new Array(),
		relatedDates: new Array(),
		relatedLabel: new Array(),

		ShowRelatedPosts: function (Label) {
			//output posts which contains Label
			//alert("Related Post num:"+this.relatedPostsNum);
			var output = "";
			var used = new Array(this.relatedPostsNum);
			for (var i = 0, idx=0; (idx < this.numRelatedPosts)&&(i < this.numPostFromRecent); i++) {
 				r=Math.floor(Math.random()*this.relatedPostsNum);
				if((used[r]!=-1)&&(this.relatedLabel[r])&&(this.relatedLabel[r].length>0)&&(this.relatedLabel[r].findIndex(Label)>=0)){
					output += '<li><a href="' + this.relatedUrls[r] + '">' + this.relatedTitles[r] + '</a> - ' + this.relatedDates[r] + '</li>';
					idx++;
				}
				used[r] = -1;
			}
			return output;

		},
		insetRelatedPosts: function(){
			var numLabel = $("span[class=post-labels]>a[href*=search/label]").length;
			//alert(numLabel);

			$("span[class=post-labels]>a[href*=search/label]").each( function(){
				//alert($(this).text());
				$(this)
				.css("color","#ff9900")
				//.css("font-size","medium")
				.css("font-weight","bold")
				.before("<br>")
				.after(relatedPost.ShowRelatedPosts($(this).text()));
			});

		}
};
function RelatedLabels (json) {

	var regex1 = new RegExp("<", "gi");
	var regex2 = new RegExp(">", "gi");
	maxPost = relatedPost.numPostFromRecent;
	if(maxPost > json.feed.entry.length)
		maxPost = json.feed.entry.length;
	for (var i = 0; (relatedPost.relatedPostsNum < maxPost)&&(i < json.feed.entry.length); i++) {

	  var entry = json.feed.entry[i];
	  relatedPost.relatedTitles[relatedPost.relatedPostsNum] = (entry.title.$t);
	  relatedPost.relatedDates[relatedPost.relatedPostsNum] = entry.published.$t.substr(0,10);
	  if(entry.category){
		  relatedPost.relatedLabel[relatedPost.relatedPostsNum] = new Array(entry.category.length);
		  for (var j = 0; j < entry.category.length; j++) {
		  		relatedPost.relatedLabel[relatedPost.relatedPostsNum][j] = entry.category[j].term;
			}
		}else{
			relatedPost.relatedLabel[relatedPost.relatedPostsNum] = null;
		}
	  for (var j = 0; j < entry.link.length; j++) {
	    if (entry.link[j].rel == 'alternate') {
	      relatedPost.relatedUrls[relatedPost.relatedPostsNum] = entry.link[j].href;
	      relatedPost.relatedPostsNum++;
	      break;
	    }
	  }
	  if((relatedPost.relatedUrls[relatedPost.relatedPostsNum-1]==document.URL)&&(relatedPost.relatedPostsNum>0))
	  	relatedPost.relatedPostsNum--;


	}
}
